#include <string.h>
#include <list.h>
#include "kingdom.h"

kSystem::kSystem(char *name, int type, int planets, int links, int x, int y) {
	sname = new char[strlen(name)];
	strcpy(sname, name);
	stype = type;
	snumberplanets = planets;
	
	splanets = new (kPlanet*)[planets];
	for(int i=0; i < snumberplanets; ++i)
		splanets[i] = 0;
	snumberlinks = links;
	
	slinks = new (kLink*)[links];
	for(int i=0; i < snumberlinks; ++i)
		slinks[i] = 0;
	
	sdefenders = 0;
	sattackers = 0;
	shidden = 0;
	
	sdefendingfleet = 0;
	sattackingfleet = 0;
	shiddenfleet = 0;
	
	sx = x;
	sy = y;
}

kSystem::~kSystem() {
	delete sname;
	delete slinks;
	for(int i=0; i < snumberplanets; ++i)
		delete splants[i];
	delete splanets;
}

const char *kSystem::name() {
	return sname;
}

const int kSystem::numberPlanets() {
	return snumberplanets;
}

const int kSystem::numberLinks() {
	return snumberLinks;
}

const kPlanet *kSystem::planet(int position) {
	return position < snumberplanets ? splanets[position] : 0;
}

const kPlanet *kSystem::planet(char *name) {
	for(int i=0; i < snumberplanets; ++i)
		if(strcmp(name, splanets[i].name()))
			return &splanets[i];
	return 0;
}

const kLink *kSystem::link(int position) {
	return position < snumberlinks ? slinks[position] : 0;
}

const kLink *kSystem::link(kSystem *system) {
	return link(system->name);
}

const kLink *kSystem::link(char *name) {
	for(int i=0; i < snumberlinks; ++i)
		if(strcmp(name, slinks[i]->one()->name) == 0 || strcmp(name, slinks[i]->two()))
			return slinks[i];
	return 0;
}

int kSystem::moveFleetToSystem(kFleet *fleet) {
	if(dominantPlayer()->number() == fleet->player()->number()) {
		fleet->next = sdefendingfleet;
		sdefendingfleet = fleet;
		sdefenders ++;
		return ENTER;
	} else {
		fleet->next = sattackingfleet;
		sattackingfleet = fleet;
		sattackers ++;
		if(detectFleet(fleet))
			return kSystem::ENTER;
		else
			return kSystem::HIDDEN;
	}
}

bool kSystem::addLink(kLink *link) {
	int i=0;
	while(slinks[i] != 0)
		++i;
	//if(i > snumberlinks)
		//return false;
	return i > snumberlinks ? slinks[i] = link, true : false;
}

//TODO: Something needs to be done with this
int kSystem::systemMoraleLevels(kPlayer *player) {
	return 100;
}

//TODO: Something needs to be done with this
bool kSystem::detectHiddenFleet(kFleet *fleet) {
	return true;
}

//TODO: Something needs to be done with this
bool kSystem::planetControlled() {
	return false;
}

char **kSystem::linkTargets() {
	char **names = new (char*)[snumberlinks];
	for(int i=0; i < snumberlinks; ++i)
		names[i] = strcmp(slinks[i]->one()->name(), sname) ? slinks[i]->two()->name() : slinks[i]->one()->name;
	return names;
}

list<kLink*> *kSystem::linksInRange(double range) {
	list<kLink*> links* = new list<kLink*>();
	for(int i=0; i < snumberlinks; ++i)
		if(slinks[i]->distance() < range)
			links.push_back(slinks[i]);
	return links;
}

//TODO: Something needs to be done with this
kPlayer *kSystem::dominantPlayer() {
	return splanets[0]->player();
}

//TODO: Something needs to be done with this
bool kSystem::detectHiddenFleet() {
	return true;
}

//TODO: Something needs to be done with return value of
//splanets[i]->carryOutOrder()
void processSystem() {
	for(int i=0; i < snumberplanets; ++i)
		splanets[i]->carryOutOrder();
}

//TODO: Something needs to be done with this
char *kSystem::starTypeName(kSystem *system) {
	return 0;
}
